new Vue({
    el: '#app',
    data() {
      return {
        template: [],
        selectOptionsOne:[],
        selectOptionsTwo: [],
      form: {
        selectOne: '',
        selectTwo: '',
        personnel:'',
        role:'',
      },
      personnel:[],
      role:[],
      templateData: [],
      currentInterfaceOptions: [],
      loading: false
      }
    },
    mounted() {
     this.getTemplate();
     this.getInterface();
    },
    computed:{
      
    },
    methods: {
    //   查询所有的模板
    getTemplate() {
      const _this = this;
      $.ajax({
        url: BASE_URL+'/weixin/getWxgTemplateMessage',
        type: 'POST',
        data: {
          'AppId': 'wx49faa81d6090e81d',
          'Appsecret': '5c771e562af5ab12a03614909852d972'
        },
        success: function (res) {
          console.log('模板',res);
          _this.template = res
          // _this.handleTemplateChange(res[0].title);
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试");
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      });
    },
    parseTemplate(templates) {
      const templateData = [];
      templates.forEach(template => {
        const regex =/\{\{\s*([^\s.]+)\.DATA\s*\}\}/g;
        let match;
        while (match = regex.exec(template.content)) {
          templateData.push({
            label: match[1], // 将中文内容作为选项
            value: '',
            selectedOption: ''
          });
        }
      });
      this.templateData = templateData;
    },
    handleTemplateChange(value) {
      const selectedTemplate = this.template.find(t => t.title === value);
      this.parseTemplate([selectedTemplate]); 
    },
    getInterface() {
      console.log('111');
      const _this = this;
      $.ajax({
        url: BASE_URL+'/weixin/getInterfaceList',
        type: 'POST',
        success: function (res) {
          console.log('接口',res);
          _this.selectOptionsTwo = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试");
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      });
    },
    handleInterfaceChange(value) {
      const selectedInterface = this.selectOptionsTwo.find(option => option.interface_Name === value);
      console.log('selectedInterface',selectedInterface);
      this.getXinInterface(selectedInterface)
    },
    getXinInterface(interface) {
      console.log('111');
      const _this = this;
      $.ajax({
        url: interface.interface_URL,
        type: 'GET',
        success: function (res) {
          console.log('新接口',res);
         _this.currentInterfaceOptions = res
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试");
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      });
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: '../addGeneralBusinessAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                enterprise_info: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './Role.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    },
  })